<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">
	    <!-- Loading Bootstrap -->
	    <link href="./Public/Admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	    <link href="./Public/Admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	    <link href="./Public/Admin/Flat/docs/assets/css/demo.css" rel="stylesheet">
	    <link rel="shortcut icon" href="./Public/Admin/Flat/img/favicon.ico">
	    <script type="text/javascript" src='bootstrap/js/bootstrap.min.js'></script>
	     <!-- 需要载入的文件 -->
	    <script type="text/javascript" src="Public/Admin/uploadify/jquery-1.8.2.min.js"></script>
	    <script type="text/javascript" src="Public/Admin/uploadify/jquery.uploadify.min.js"></script>
	    <link rel="stylesheet" type="text/css" href="Public/Admin/uploadify/uploadify.css">

	    <!--载入百度编辑器-->
	    	<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/ueditor.config.js"></script>
    		<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/ueditor.all.min.js"> </script>
    		<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>

	     <script>
	   	  $(function(){
	   	  	//抓取时间源,选中事件
	   	  	$('select[name=category_cid]').change(function(){
	   	  		// 获取被选中的tid
	   	  		var tid= $('option:selected').attr('tid');
	   	  		// console.log(tid);
	   	  		// alert(1);
	   	  		//传参
	   	  		$.ajax({
	   	  			type:"post",
	   	  			url:"<?php echo U('Goods/getAttr')?>",
	   	  			data:{t : tid},
	   	  			dataType:'json',
	   	  			success:function(phpData){
	   	  				// console.log(phpData);
	   	  				var attr="";
	   	  				var spec="";
	   	  				// 循环
	   	  				$.each(phpData,function(k,v){
	   	  					// 判断是属性
	   	  					// 
	   	  					if(v.class==0){
	   	  						attr+='<tr class="info">';
	   	  						attr+='<td>'+v.taname+'</td>';
	   	  						attr+='<td>';
	   	  						attr+='<select name="attr['+v.taid+']" id="">';
	   	  						attr+='<option value="">请选择</option>';
		   	  						 $.each(v.tavalue, function(kk,vv) { 
										attr += '<option value="'+vv+'">'+vv+'</option>';
									 });
		   	  					attr+='</select></td></tr>';
	   	  					}
	   	  					// // 还是规格
	   	  					 else{
	   	  					 	spec+='<tr class="info" >';
	   	  					 	spec+='<td>'+v.taname+'</td>';
	   	  					 	// console.log(v);
	   	  					 	spec+='<td>';
	   	  					 	spec+='<select name="spec[value]['+v.taid+'][]" id="" class="form-control">';
	   	  					 	spec+='<option value="">请选择</option>';
		   	  					 	$.each(v.tavalue,function(kk,vv){
		   	  					 	  	 spec+='<option value="'+vv+'">'+vv+'</option>';
		   	  					 	});
	   	  					 	spec+='</select></td><td>附加价格：';
	   	  					 	spec+='<input type="text" name="spec[added]['+v.taid+'][]" id=""/>';
	   	  					 	spec+='</td><td>';
	   	  					 	spec+='<a href="javascript:;" class="btn btn-primary addSpec">添加规格</a>';
	   	  					 	spec+='</td></tr>';

	   	  					 }
	   	  				});
   	  					$('#attr').html(attr);
   	  					$('#spec').html(spec);
	   	  				
	   	  			}
	   	  		 })
	   	  		});
	   	  	
	   	  	$('.addSpec').live('click',function(){
	   	  		// alert(1);
	   	  		// 克隆当前的父级
	   	  		var trObj = $(this).parents('tr');
				//克隆一个
				var tr = trObj.clone();
				// 改变按钮文字
				tr.find('.addSpec').html('删除规格').removeClass('btn-primary addSpec').addClass('btn-info removeSpec');
				trObj.after(tr);
	   	  	})
	   	  	$('.removeSpec').live('click',function(){
	   	  		$(this).parents('tr').remove();
	   	  	})

	   	  })

	   </script>
	</head>
	<body>
		<form action="" method="post">
			<div class="alert alert-success">编辑商品</div>
			<div class="form-group">
				<label for="exampleInputEmail1">所属分类</label>
				<select name="category_cid" class="form-control ">
					<?php foreach ($cateData as $v){?>
						<option value="<?php echo $v['cid']?>" tid="<?php echo $v['type_tid']?>"
						<?php if($good['category_cid'] == $v['cid']) :?>
							selected
						<?php endif ?> ><?php echo $v['_name']?> 
						</option>
					<?php }?>
					<!-- <input type="hidden" name="type_tid" value="<?php echo $v['type_tid']?>"> -->
				</select>

			</div>

			<div class="form-group">
				<label for="exampleInputEmail1">所属品牌</label>
				<select name="brand_bid" class="form-control">
					<?php foreach ($brandData as $v){?>
						<option value="<?php echo $v['bid']?>" 
						<?php if($good['brand_bid'] == $v['bid'] ):?>
							selected
						<?php endif ?>
						><?php echo $v['bname']?></option>
					<?php }?>
				</select>
			</div>
			<div class="form-group">
				<label for="exampleInputEmail1">商品名称</label>
				<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入分类名称" required="" name="gname" value="<?php echo $good['gname']?>">
			</div>
			<div class="form-group">
				<label for="exampleInputEmail1">单位</label>
				<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入分类名称" required="" name="unit" value="<?php echo $good['unit']?>">
			</div>
			<div class="form-group">
				<label for="exampleInputEmail1">市场价</label>
				<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入分类标题" required="" name="marketprice" value="<?php echo $good['marketprice']?>">
			</div>
			<div class="form-group">
				<label for="exampleInputEmail1">商城价</label>
				<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入分类名称" required="" name="shopprice" value="<?php echo $good['shopprice']?>">
			</div>
			<div class="form-group">
				<label for="exampleInputEmail1">总库存</label>
				<input id="exampleInputEmail1" class="form-control" type="text" placeholder="请输入分类名称" required="" name="stock" value="<?php echo $good['stock']?>">
			</div>
			<p class="btn btn-primary" ">商品属性</p>
			<table class="table table-bordered table-hover" id='attr' class="info">
				<?php foreach ($goodData as $v){?>
				<!-- <?php echo p($v)?>; -->
					<?php if($v['class']==0){?>
                
					<tr class="info" >
					 	<td><?php echo $v['taname']?></td>
						<td>
							<select name="attr[<?php echo $v['taid']?>]" id="" class="form-control">
								<option value="">请选择</option>
								<?php foreach ($v['tavalue'] as $value){?>
								<option value="<?php echo $value?>"
									<?php if($v['gtvalue']==$value){?>
                
										selected
									
               <?php }?>
								>
									<?php echo $value?>
								</option>
								<?php }?>
							</select>
						</td>
					</tr>
					
               <?php }?>
				<?php }?>
			</table>
		
			
			<p class="btn btn-primary">商品规格</p>
			<table class="table table-bordered table-hover" id='spec'>
				<?php foreach ($goodData as $v){?>
					<?php if($v['class']==1){?>
                
						<?php
							// 把属于规格的属性id压过来
							$taid[]=$v['taid'];
						?>
					<tr class="info" >
					 	<td><?php echo $v['taname']?></td>
						<td>
							<select name="spec[value][<?php echo $v['taid']?>][]" id="" class="form-control">
								<option value="">请选择</option>
								<?php foreach ($v['tavalue'] as $value){?>
								<option value="<?php echo $value?>"
									<?php if($v['gtvalue']==$value){?>
                
										selected
									
               <?php }?>
								>
									<?php echo $value?>
								</option>
								<?php }?>
							</select>
						</td>
						<td>附加价格： <input type="text" value="<?php if(isset($v['added'])){echo $v['added'];}else{echo 0;} ?>"name="spec[added][<?php echo $v['taid']?>][]"></td>
						<td>
							<a href="javascript:;" class="btn btn-info removeSpec goodAttr<?php echo $v['taid']?>">删除规格</a>
						</td>
					</tr>
					
               <?php }?>
				<?php }?>
						
			</table>
			<?php
			//去除重复的taid
			$taid =array_unique($taid);
			?>
			<!-- <?php echo p($taid);?> -->
			<script>
				// json格式传入到jq
				var taid='<?php echo json_encode($taid) ?>';
				// alert(taid);
				// 把js字符串转化为json对象
				taid=$.parseJSON(taid);
				// alert(taid);
				$.each(taid,function(k,v)
					{
					$('.goodAttr'+v).first().removeClass('btn-info removeSpec').addClass('btn-primary addSpec').html('添加规格');
				})
			</script>
			<!-- 上传图片 -->
			<!-- html页面 -->
	
<div lab="uploadFile">
	<!-- file表单 -->
    <input id="f" type="file" multiple="true">
    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
    <script type="text/javascript">
        $(function() {

            $('#f').uploadify({

                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '<?php echo __PUBLIC__?>/Admin/uploadify/uploadify.swf',
                'uploader' : '<?php echo U('upload')?>',//指定服务器上传的方法
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 1,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
                    //把php返回的数据转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li>";
                    li += "<img src='"+imageUrl+"'style='width:200px;height:100px;''/>";
                    li += "<input type='hidden' name='pic' value='"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
                    li += "</li>";
                    $("#uploadLists ul").prepend(li);
                    $("#img").remove();
                }
            });

			//关闭图片
            var i = 1;
            $('#self-close').live('click',function(){
                $(this).parent('li').remove();
                i++;
                $('#f').uploadify('settings','uploadLimit',i);
            })
        });
    </script>

    <div id="uploadLists">
        <ul>

        </ul>
    </div>

</div>

<!-- 百度编辑器 -->
		<div class="form-group">
			<label for="exampleInputEmail1">商品详细</label>
			<!--调用百度编辑器-->
			<script id="editor" type="text/plain" style="width:100%;height:500px;" name="det"></script>
		    <script>
		        var ue = UE.getEditor('editor');
		    </script>
		</div>
		

<!-- 缩略图上传 -->
<div lab="uploadFile">
	<!-- file表单 -->
    <input id="t" type="file" multiple="true">
    <span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
    <script type="text/javascript">
        $(function() {
            $('#t').uploadify({
                'formData'     : {//POST数据
                    '<?php echo session_name();?>' : '<?php echo session_id();?>',
                },
                'fileTypeExts' : '*.jpg;*.png',
                'swf'      : '<?php echo __PUBLIC__?>/Admin/uploadify/uploadify.swf',
                'uploader' : '<?php echo U('upload')?>',//指定服务器上传的方法
                'buttonText':'选择文件',
                'fileSizeLimit' : '2000KB',
                'uploadLimit' : 1,//上传文件数
                'width':65,
                'height':25,
                'successTimeout':10,//等待服务器响应时间
                'removeTimeout' : 0.2,//成功显示时间
                'onUploadSuccess' : function(file, data, response) {
                    //把php返回的数据转为json
                    data=$.parseJSON(data);
                    //获得图片地址
                    var imageUrl = data.url;
                    var li="<li>";
                    li += "<img src='"+imageUrl+"'/>";
                    li += "<input type='hidden' name='thumb[]' value='"+data.path+"'/><a href='javascript:;' id='self-close'>X</a>";
                    li += "</li>";
                    $("#uploadList ul").prepend(li);
                }
            });

			//关闭图片
            // var i = 1;
            $('#self-close').live('click',function(){

            	
            	// var a=$("#zcj").attr('src');alert(a);
                $(this).parent('li').remove();
                // i++;
                // $('#t').uploadify('settings','uploadLimit',i);
            })
        });
    </script>
    <div id="uploadList"> 
        <ul>
        <?php foreach ($good['thumb'] as $v){?>
	        <li>
	        
	        
				<img id="zcj" src="<?php echo $v?>"/>
			
				<input type='hidden' name='thumb' value="<?php echo $v?>" />
				<a href='javascript:;' id='self-close'>X</a>

			</li>
		<?php }?>
        </ul>
    </div>
</div>

			<input type="hidden" name="gid" value="<?php echo $good['gid']?>">
			<button class="btn btn-primary btn-block" type="submit"> 确定 </button>
		</form>
	</body>
</html>
